﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace FreeProperty
{
    public static class PagingUtil
    {
        public static int PageSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PageSize"]);
        public static int MaxPageCount = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["MaxPageCount"]);
        public static int GetPagesCount(int ItemsCount)
        {
            int NumberOfPages = 1;
            if (ItemsCount % PageSize == 0)
            {
                NumberOfPages = ItemsCount / PageSize;
            }
            else
            {
                NumberOfPages = ItemsCount / PageSize + 1;
            }
            return NumberOfPages;
        }
        public static void DoPaging(string url,string type,HtmlGenericControl spanFootServerPaging,HiddenField hdnCurrentPage, HiddenField hdnPageCount, HtmlAnchor lnkFootNext, HtmlAnchor lnkFootLast, HtmlAnchor lnkFootFirst, HtmlAnchor lnkFootPrevious)
        {
            int startNumber = 0;
            int endNumber = 0;
            StringBuilder strPaging = new StringBuilder();
            //int CurrentPage = Convert.ToInt32(hdnCurrentPage.Value) - 1;
            int CurrentPage = Convert.ToInt32(hdnCurrentPage.Value) ;
            int CurrentPageIndex = CurrentPage-1 ;
            int TotalPageCount = Convert.ToInt32(hdnPageCount.Value);
            

            lnkFootNext.Visible = true;
            lnkFootLast.Visible = true;
            lnkFootFirst.Visible = true;
            lnkFootPrevious.Visible = true;

            if (CurrentPage == 1)
            {
                lnkFootFirst.Visible = false;
                lnkFootPrevious.Visible = false;
            }
            if (CurrentPage == TotalPageCount)
            {
                lnkFootNext.Visible = false;
                lnkFootLast.Visible = false;
            }
            if (TotalPageCount <= MaxPageCount)
            {
                endNumber = TotalPageCount -1;
                startNumber = 0;
            }
            else
            {
                startNumber = CurrentPageIndex - ((int)(CurrentPageIndex % PageSize));
                if (TotalPageCount > (startNumber + PageSize - 1))
                    endNumber = startNumber + PageSize - 1;
                else
                    endNumber = TotalPageCount - 1;
            }

            for (int i = startNumber; i <= endNumber; i++)
            {
                if (CurrentPageIndex == i)
                    strPaging.Append("<b>" + ((i + 1) + "&nbsp;") + "</b>");
                else
                    strPaging.Append("<a href='#' style='cursor:hand' onclick=javascript:frmSearchCommandArgument('" + (i + 1) + "','"+url+"','frmContent','"+type+"');>" + ((i + 1) + "&nbsp;") + "</a>");

            }

            spanFootServerPaging.InnerHtml = strPaging.ToString();

            lnkFootPrevious.Attributes.Add("onclick", "javascript:frmSearchPaging('Previous','"+url+"','frmContent','"+type+"');");
            lnkFootNext.Attributes.Add("onclick", "javascript:frmSearchPaging('Next','" + url + "','frmContent','" + type + "');");
            lnkFootFirst.Attributes.Add("onclick", "javascript:frmSearchPaging('First','" + url + "','frmContent','" + type + "');");
            lnkFootLast.Attributes.Add("onclick", "javascript:frmSearchPaging('Last','" + url + "','frmContent','" + type + "');");
        }
        public static void BindDataToGrid(GridView gvData,object data,string url,string type,HtmlGenericControl spanFootServerPaging,HiddenField hdnCurrentPage, HiddenField hdnPageCount, HtmlAnchor lnkFootNext, HtmlAnchor lnkFootLast, HtmlAnchor lnkFootFirst, HtmlAnchor lnkFootPrevious)
        {
            gvData.DataSource = data;
            gvData.DataBind();
            PagingUtil.DoPaging(url, "AD", spanFootServerPaging, hdnCurrentPage, hdnPageCount, lnkFootNext, lnkFootLast, lnkFootFirst, lnkFootPrevious);

        }
    }
    
}